<?php
declare(strict_types=1);

use LowCode\AutoId;
use LowCode\Core\Admin;
use LowCode\Support\JsExp;
use LowCode\Support\JsFn;

if (!function_exists('getLowCodePath')) {
    /**
     * @param string $path
     * @param string $basePath
     * @return string
     */
    function getLowCodePath(string $path = "", string $basePath = ""): string
    {
        $path = trim($path,DIRECTORY_SEPARATOR);
        $basePath = trim($basePath,DIRECTORY_SEPARATOR);
        if ($basePath){
            return __DIR__.DIRECTORY_SEPARATOR.$basePath.DIRECTORY_SEPARATOR.$path;
        }else{
            return __DIR__.DIRECTORY_SEPARATOR.$path;
        }
    }
}

if (!function_exists('getPublicPath')) {
    /**
     * @param string $path
     * @param string $basePath
     * @return string
     */
    function getPublicPath(string $path, string $basePath = ""): string
    {
        if (isRunInLaravel()){
            return public_path($path);
        }
        return Admin::publicPath($path, $basePath);
    }
}

if (!function_exists('isRunInLaravel')){
    /**
     * @return bool
     */
    function isRunInLaravel(): bool
    {
        try {
            if (class_exists('Illuminate\Foundation\Application') || function_exists('app')){
                $app = app();
                return method_exists($app,'hasBeenBootstrapped') && $app->hasBeenBootstrapped() && defined('LARAVEL_START');
            }else{
                return false;
            }
        }catch (Exception){
            return false;
        }
    }
}

if (! function_exists('class_basename')) {
    /**
     * Get the class "basename" of the given object / class.
     *
     * @param  string|object  $class
     * @return string
     */
    function class_basename(string|object $class): string
    {
        $class = is_object($class) ? get_class($class) : $class;

        return basename(str_replace('\\', DIRECTORY_SEPARATOR, $class));
    }
}

if (!function_exists("randomId")){
    /**
     * @param int $length
     * @param string $prefix
     * @return string
     */
    function randomId(int $length = 32, string $prefix = ""): string
    {
        $uniqueId = uniqid($prefix, true);
        return str_replace(".","",substr($uniqueId, 0, $length));
    }
}

if (!function_exists("convertToCamelCase")){
    /**
     * @param string $string
     * @return string
     */
    function convertToCamelCase(string $string): string
    {
        // 将字符串转换成首字母小写，其余单词首字母大写的形式
        return ucfirst(str_replace(' ', '', ucwords(str_replace(['_','-'], ' ', $string))));
    }
}


if (!function_exists("jsExp")){
    /**
     * @param string $value
     * @param string|null $id
     * @return JsExp
     */
    function jsExp(string $value, string $id = null): JsExp
    {
        return JsExp::make($value, $id);
    }
}

if (!function_exists("jsFn")){
    /**
     * @param string $value
     * @param string|null $id
     * @return JsFn
     */
    function jsFn(string $value, string $id = null): JsFn
    {
        return JsFn::make($value, $id);
    }
}

if (!function_exists("func")){
    /**
     * @param string $value
     * @return string
     */
    function func(string $value): string
    {
        return "()=>{$value}";
    }
}

if (!function_exists("autoId")){
    /**
     * @param string|null $prefix
     * @return string
     */
    function autoId(string $prefix = null): string
    {
        return AutoId::make($prefix);
    }
}
